﻿@inject IHttpService _httpService
@inject IPageModelCatche<List< AnnouncementItemModel>> _pageModelCatche
@inject NavigationManager NavigationManager

<CnGalWebSite.Components.Containers.MiniModeContainer>
    <section class="announcements-card">
        <h2>公告</h2>
        <div class="group">
            @foreach (var item in _items)
            {
                <MDCard Class="item" Href="@item.Url">
                    @if (GetIcon(item) != null)
                    {
                        <MIcon Color="@GetIconColor(item)">@GetIcon(item)</MIcon>
                    }

                    <div class="content">
                        <div class="name text-truncate-1 @(GetIcon(item) != null?"active":"")">@item.Name</div>
                    </div>

                </MDCard>
            }
        </div>
    </section>
</CnGalWebSite.Components.Containers.MiniModeContainer>


@code {

    List<AnnouncementItemModel> _items { get; set; } = new List<AnnouncementItemModel>();

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }


    protected override async Task OnInitializedAsync()
    {
        try
        {
            //获取主页信息
            _items = await _pageModelCatche.GetCache("api/home/ListAnnouncements");
            StateHasChanged();
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "获取主页公告列表失败");
        }
    }

    public string GetIcon(AnnouncementItemModel model)
    {
        if (model.Priority != 0)
        {
            return IconType.Pin.ToIconString();
        }

        if (model == _items.FirstOrDefault(s => s.Priority == 0))
        {
            return IconType.New.ToIconString();
        }

        return null;
    }

    public string GetIconColor(AnnouncementItemModel model)
    {
        if (model.Priority != 0)
        {
            return "warning";
        }

        if (model == _items.FirstOrDefault(s => s.Priority == 0))
        {
            return "success";
        }

        return null;
    }
}
